package com.zys.controller;

import java.util.UUID;
import java.util.Objects;

import com.zys.pojo.Good;
import com.zys.entity.Result;
import com.zys.utils.QiniuUtils;
import com.zys.entity.QueryInfo;
import com.zys.entity.PageResult;
import com.zys.service.GoodService;
import com.zys.constant.MessageConstant;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 处理商品相关操作
 */
@RestController
@RequestMapping("/goods")
public class GoodController {

  @Reference
  private GoodService goodService;

  /**
   * 根据条件获取商品列表
   *
   * @param queryInfo 查询信息(条件)
   * @return PageResult
   */
  @RequestMapping("/getGoodsList")
  public PageResult<Good> getUserList(@RequestBody QueryInfo queryInfo) {
    try {
      return goodService.getGoodsList(queryInfo);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 图片上传处理
   *
   * @param goodsImg 上传的图片对象
   * @return Result
   */
  @RequestMapping("/upload")
  public Result upload(@RequestParam("goodsImg") MultipartFile goodsImg) {
    try {
      String originalFilename = goodsImg.getOriginalFilename();     // 获取原文件名
      // 随机生成文件名
      String fileName = UUID.randomUUID() +
          Objects.requireNonNull(originalFilename)
              .substring(originalFilename.lastIndexOf(".") - 1);

      QiniuUtils.upload2Qiniu(goodsImg.getBytes(), fileName);       // 上传到七牛云

      return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }
  }

  /**
   * 添加商品
   *
   * @param id   分类 id
   * @param good 商品信息
   * @return Result
   */
  @RequestMapping("/add")
  public Result add(@RequestParam Integer id, @RequestBody Good good) {
    try {
      goodService.add(id, good);
      return new Result(true, MessageConstant.ADD_GOODS_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.ADD_GOODS_FAIL);
    }
  }

  /**
   * 删除商品
   *
   * @param id 商品 id
   * @return Result
   */
  @RequestMapping("/delete")
  public Result delete(@RequestParam Integer id) {
    try {
      goodService.delete(id);
      return new Result(true, MessageConstant.DELETE_GOODS_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.DELETE_GOODS_FAIL);
    }
  }

  /**
   * 根据 id 获取指定的商品信息
   *
   * @param id 商品 id
   * @return Result
   */
  @RequestMapping("/getGoodById")
  public Result getGoodById(Integer id) {
    try {
      return new Result(true, MessageConstant.GET_GOODS_SUCCESS, goodService.findGoodById(id));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_GOODS_FAIL);
    }
  }

  /**
   * 修改商品信息
   *
   * @param good 修改后的商品信息
   * @return Result
   */
  @RequestMapping("/edit")
  public Result edit(@RequestBody Good good) {
    try {
      goodService.edit(good);
      return new Result(true, MessageConstant.EDIT_GOODS_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.EDIT_GOODS_FAIL);
    }
  }
}
